#include "teacher.h"
#include <QDebug>
teacher::teacher()
{
    this->setM=new modelSet;
    this->file1=new file;
}


void teacher::setInfo(QString name, QString id, QString password, QString college, QString level, QStringList course)
{
    this->person::setInfo(name,id);
    this->teaCollege=college;
    this->teaLevel=level;
    this->teaPassword=password;
    this->teaCourse=course;

}

void teacher::loadInfo()
{
    //加载全校课程信息
    QStringList _line1;
    this->file1->readFile("course.csv",_line1);
    for(int i=0;i<_line1.length();i++)
    {
        QString line=_line1.at(i);
        line=line.trimmed();
        QStringList linesplit=line.split(",");
        course a;
        a.setCourseInfo(linesplit.at(0),linesplit.at(1),linesplit.at(2),linesplit.at(3),linesplit.at(4),linesplit.at(5),linesplit.at(6),linesplit.at(7),linesplit.at(8),linesplit.at(9),linesplit.at(10),linesplit.at(11),linesplit.at(12));
        allCourse.push_back(a);
    }
    //加载个人课程信息到类中
    for(int i=0;i<teaCourse.length();i++)
    {
        myCourse.push_back(matchCourseWithId(teaCourse.at(i),allCourse));
    }
    //加载所有学生
    QStringList _line2;
    this->file1->readFile("student.csv",_line2);
    for(int i=0;i<_line2.length();i++)
    {
        QString line=_line2.at(i);
        QStringList selectedCourse;
        line=line.trimmed();
        QStringList linesplit=line.split(",");
        for(int i=7;i<linesplit.length();i++)
        {
            selectedCourse.append(linesplit.at(i));
        }
        student a;
        a.setInfo(linesplit.at(0),linesplit.at(1),linesplit.at(3),linesplit.at(4),linesplit.at(5),linesplit.at(6),linesplit.at(2),selectedCourse);
        allStudent.push_back(a);
    }
}

void teacher::loadScore()
{
    //加载成绩信息
    QStringList _line;
    this->file1->readFile("score.csv",_line);
    for(int i=0;i<_line.length();i++)
    {
        QString line=_line.at(i);
        QStringList score;
        line=line.trimmed();
        QStringList linesplit=line.split(",");
        for(int i=2;i<linesplit.length();i++)
        {
            score.append(linesplit.at(i));
        }
        student a;
        a.setScore(linesplit.at(0),linesplit.at(1),score);
        StudentScore.push_back(a);
    }

}

bool teacher::loadMyStudent(QString selectId)
{
    myStudent.clear();
    for(int i=0;i<allStudent.length();i++)
    {
        for(int j=0;j<allStudent.at(i).getSelectedCourse().length();j++)
        {
            if(selectId==allStudent.at(i).getSelectedCourse().at(j))
            {
                myStudent.push_back(allStudent.at(i));
            }
        }
    }
    if(myStudent.size()==0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

bool teacher::modifyPassword(QString oldPassword, QString newPassword)
{
    if(oldPassword==this->teaPassword)
    {
        if(newPassword.length()<6)
        {
            return false;
        }
        else
        {
            this->teaPassword=newPassword;
            return  true;
        }
    }
    else
    {
        return false;
    }
}

void teacher::checkMyInfo(QStandardItemModel *model, int curRow) const
{
    QStringList line;
    line.append(this->getName());
    line.append(this->getId());
    line.append(this->teaCollege);
    line.append(this->teaLevel);
    line.append(this->teaCourse);
    this->setM->setTeacherTitle(model);
    this->setM->setTeacherItem(model,line,curRow);
}

void teacher::checkMyStudent(QStandardItemModel *model)
{
    for(int i=0;i<myStudent.length();i++)
    {
        myStudent.at(i).checkMyInfo(model,i);
    }
}

bool cmp_avg1(student a, student b)//加权平均分升序
{
    return a.getAvg()> b.getAvg();
}

bool cmp_avg2(student a, student b)//加权平均分降序
{
    return a.getAvg()< b.getAvg();
}

void teacher::sortScoreDown(QStandardItemModel *model)//加权平均分升序
{
    std::sort(StudentScore.begin(), StudentScore.end(), cmp_avg1);
    for(int i=0;i<StudentScore.length();i++)
    {
        StudentScore.at(i).checkMyScore(model,i);
    }
}

void teacher::sortScoreUp(QStandardItemModel *model)//加权平均分降序
{
    std::sort(StudentScore.begin(), StudentScore.end(), cmp_avg2);
    for(int i=0;i<StudentScore.length();i++)
    {
        StudentScore.at(i).checkMyScore(model,i);
    }
}

void teacher::checkScore(QStandardItemModel *model)
{
    for(int i=0;i<StudentScore.length();i++)
    {
        StudentScore.at(i).checkMyScore(model,i);
    }
}

void teacher::checkMyCourse(QStandardItemModel *model)
{
    this->setM->setCourseTitle(model);
    for(int i=0;i<myCourse.length();i++)
    {
        myCourse.at(i).displayThisCourse(model,i);
    }
}

QStringList teacher::getMyCourse()
{
    return this->teaCourse;
}

QStringList teacher::getAllInfo() const
{
    QStringList allInfo;
    allInfo.append(getName());
    allInfo.append(getId());
    allInfo.append(teaPassword);
    allInfo.append(teaCollege);
    allInfo.append(teaLevel);
    allInfo.append(teaCourse);
    return allInfo;
}

QString teacher::getCollege()
{
    return teaCollege;
}

QString teacher::getLevel()
{
    return teaLevel;
}

void teacher::saveAllInfo()
{
    this->file1->modifyFile("teacher",getAllInfo());
    for(int i =0;i<waitToLoadCourse.length();i++)
    {
        this->file1->appendToFile("waitToLoadCourse",waitToLoadCourse.at(i).getAllInfo());
    }
}

void teacher::addCourse(QString id)
{
    teaCourse.append(id);
}

void teacher::removeCourse(QString id)
{
    for(int i=0;i<teaCourse.length();i++)
    {
        if(teaCourse.at(i)==id)
        {
            teaCourse.removeAt(i);
        }
    }
}

void teacher::submitCourse(course &course)
{
    waitToLoadCourse.push_back(course);
}

course &teacher::matchCourseWithId(const QString courseSelectId, QVector<course> &course)
{
    for(int i=0;i<course.length();i++)
    {
        if(course.at(i).getCourseSelectId()==courseSelectId)
        {
            return course[i];
        }
    }
}
